package com.yyk.yykywd.ui.work;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yykywd.R;
import com.yyk.yykywd.listenter.DialogSureCancelListenter;
import com.yyk.yykywd.util.DialogUtil;
import com.yyk.yykywd.util.OnClickFlagUtil;
import com.yyk.yykywd.util.SingleOptionsPickerUtil;
import com.yyk.yykywd.base.BaseActivity;
import com.yyk.yykywd.config.Constant;
import com.yyk.yykywd.http.ApiService;
import com.yyk.yykywd.bean.WorkBusinessreturnedInfo;
import com.yyk.yykywd.ui.MainActivity;
import com.yyk.yykywd.util.Md5Util2;
import com.yyk.yykywd.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 添加回款信息
 */
public class AddReceivableActivity extends BaseActivity {

    @BindView(R.id.bt_save)
    Button btSave;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.ll_type)
    LinearLayout llType;
    @BindView(R.id.ll_showone)
    LinearLayout llShowone;
    @BindView(R.id.ll_showtwo)
    LinearLayout llShowtwo;
    @BindView(R.id.ll_showthree)
    LinearLayout llShowthree;
    @BindView(R.id.ll_showfour)
    LinearLayout llShowfour;
    @BindView(R.id.et_remark)
    EditText etRemark;
    @BindView(R.id.et_moeny)
    EditText etMoeny;
    @BindView(R.id.et_xu_money)
    EditText etXuMoney;
    @BindView(R.id.et_paymoney)
    EditText etPaymoney;
    private String id;
    private int typeValue = 1;

    @Override
    public int getLayoutId() {
        return R.layout.activity_add_receivable;
    }

    @Override
    public void initView() {
        initToolbar();
    }

    private void initToolbar() {
        setBackArrow();
        setTitle("回款信息");
    }

    @Override
    public void initData() {
        initGetIntent();
    }

    private void initGetIntent() {
        if (getBundle() != null) {
            id = getBundle().getString("id");
        }
    }

    /**
     * 接口18：添加回款信息（whm）
     */
    private void postWorkBusinessreturned(final boolean flag) {
        if (typeValue == 1) {
            if (TextUtils.isEmpty(etMoeny.getText())) {
                ToastUtil.showShort("请填写回款金额");
                return;
            }
        } else if (typeValue == 2) {
            if (TextUtils.isEmpty(etXuMoney.getText())) {
                ToastUtil.showShort("请填写需回款金额");
                return;
            }
            if (TextUtils.isEmpty(etPaymoney.getText())) {
                ToastUtil.showShort("请填写已回款金额");
                return;
            }
        } else {

        }

        if (TextUtils.isEmpty(etRemark.getText())) {
            ToastUtil.showShort("请填写相关描述");
            return;
        }

        DialogUtil.showReturnTips(mActivity, "业务完成",
                "保存后，此条业务将完成，您可在“业务统计”中的”合作成功“模块中查看此条业务", "确定", "取消", new DialogSureCancelListenter() {


            @Override
            public void onSureListenter() {
                Map<String, String> map = new TreeMap<>();
                map.put("uid", Hawk.get("uid")+"");
                map.put("bid", id + "");
                map.put("remark", etRemark.getText().toString().trim() + "");
                map.put("classes", typeValue + "");
                if (typeValue == 1) {
                    map.put("money", etMoeny.getText().toString() + "");//回款金额
                } else if (typeValue == 2) {
                    map.put("money", etXuMoney.getText().toString() + "");//回款金额
                    map.put("pay_money", etPaymoney.getText().toString() + "");
                } else {
                    //不需回款 不做任何操作
                }

                map.put("sign", Md5Util2.createSign(map));
                ApiService.getInstance().api.postWorkBusinessreturned(map)
                        .subscribeOn(Schedulers.io())     //调度线程
                        .observeOn(AndroidSchedulers.mainThread())
                        .unsubscribeOn(Schedulers.io())   //取消订阅
                        .subscribe(new Observer<WorkBusinessreturnedInfo>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(WorkBusinessreturnedInfo info) {
                                Logger.e("添加回款信息" + info.toString());
                                if (info.getCode() == 1) {
                                    startAct(MainActivity.class, true);
                                } else {
                                    ToastUtil.showShort(info.getMsg());

                                }
                            }

                            @Override
                            public void onError(Throwable e) {
                            }

                            @Override
                            public void onComplete() {

                            }
                        });

            }

            @Override
            public void onCancelListenter() {
            }
        }, false);

    }


    @OnClick({R.id.bt_save, R.id.ll_type})
    public void onViewClicked(View view) {
        if (OnClickFlagUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.bt_save:
                postWorkBusinessreturned(true);
                break;
            case R.id.ll_type:
                List<String> list = new ArrayList<>();
                list.add("全部回款");
                list.add("部分回款");
                list.add("不需回款");
                SingleOptionsPickerUtil.openOptionsPicker(this, list, 1, tvType);
                SingleOptionsPickerUtil.setOnPicker(new SingleOptionsPickerUtil.Listenter() {
                    @Override
                    public void lisenter(String value) {
                        if (value.equals("部分回款")) {
                            llShowthree.setVisibility(View.VISIBLE);
                            llShowtwo.setVisibility(View.VISIBLE);
                            llShowone.setVisibility(View.GONE);
                            llShowfour.setVisibility(View.VISIBLE);
                            typeValue = 2;
                        } else if (value.equals("不需回款")) {
                            llShowthree.setVisibility(View.GONE);
                            llShowtwo.setVisibility(View.GONE);
                            llShowone.setVisibility(View.GONE);
                            llShowfour.setVisibility(View.VISIBLE);
                            typeValue = 3;
                        } else {
                            llShowthree.setVisibility(View.GONE);
                            llShowtwo.setVisibility(View.GONE);
                            llShowone.setVisibility(View.VISIBLE);
                            llShowfour.setVisibility(View.VISIBLE);
                            typeValue = 1;
                        }
                    }
                });

                break;
        }
    }


}
